package hit.edu.zjc.UserInterface;

import java.util.ArrayList;

public class UDDoubleType extends UserDef {

	
	public static final int MAXFILL=101;
	public static final int MINFILL=102;
	public static final int AVEFILL=103;
	public static final int MOSTFREQUENTFILL=104;
	public static final int LEASTFREQUENTFILL=105;
	
	public double Max;
	public boolean check_Max;
	public double Min;
	public boolean check_Min;
	public double CorrectValue;
	
	//不处理
	public UDDoubleType()
	{
		super(DoubleType,false,false,true);
	}
	//缺失值填充
	public UDDoubleType(double max,boolean cmax,double min,boolean cmin,int ft,int[] cid)
	{
		super(DoubleType,true,false,false);
		FillType=ft;
		Max=max;
		Min=min;
		check_Max=cmax;
		check_Min=cmin;
		RelevantColumID=new int[cid.length];
		for(int i=0;i<cid.length;i++)
		{
			RelevantColumID[i]=cid[i];
		}
	}
	//EasyFill
	public UDDoubleType(boolean ischeck,boolean in,double max,boolean cmax,double min,boolean cmin,double cv) {
		super(DoubleType, ischeck, in,true);
		Max=max;
		Min=min;
		check_Max=cmax;
		check_Min=cmin;
		CorrectValue=cv;
	}
	
	
	//EasyFill 不一致修复
	public UDDoubleType(boolean ischeck,boolean in,double max,boolean cmax,double min,boolean cmin,double cv,int ft) {
		super(DoubleType, ischeck, in,true);
		Max=max;
		Min=min;
		check_Max=cmax;
		check_Min=cmin;
		CorrectValue=cv;
		FillType=ft;
	}

	@Override
	public boolean checkdata(String ob) {
		Double double_ob;
		try
		{
			double_ob=Double.parseDouble(ob);
		}
		catch(Exception e)
		{
			return false;//数据格式非法，直接返回false。
		}
		if(double_ob==null)
		{
			if(isNull)
				return true;
			else
				return false;
		}
		if(check_Max&&double_ob.compareTo(Max)>0)
			return false;
		if(check_Min&&double_ob.compareTo(Min)<0)
			return false;
		
		return true;
	}

	@Override
	public String correctdata() {
		if(isEasyFill)
		{
			return new Double(CorrectValue).toString();
		}
		else
			return null;
	}

	@Override
	public String complexFill(String[] data)
	{
		if(FillType==EASYFILL)
			return new Double(CorrectValue).toString();
		if(data.length==0)
			return null;
		ArrayList<String>strlist=new ArrayList<String>(data.length);
		for(int i=0;i<data.length;i++)
		{
			if(data[i]==null||data[i].trim().equals(""))
				continue;
			strlist.add(data[i]);
		}
		if(strlist.size()==0)
			return null;
		Double[] value=new Double[strlist.size()];
		for(int i=0;i<value.length;i++)
		{
			value[i]=Double.parseDouble(strlist.get(i));
		}
		if(FillType==MAXFILL)//最大值填充
		{
			int maxindex=0;
			for(int i=1;i<value.length;i++)
			{
				if(Double.compare(value[i],value[maxindex])>0)
					maxindex=i;
			}
			return value[maxindex].toString();
		}
		else if(FillType==MINFILL)
		{
			int minindex=0;
			for(int i=1;i<value.length;i++)
			{
				if(Double.compare(value[i],value[minindex])<0)
					minindex=i;
			}
			return value[minindex].toString();
		}
		else if(FillType==AVEFILL)
		{
			double sum=0;
			for(int i=0;i<value.length;i++)
			{
				sum=sum+value[i];
			}
			double ave=sum/value.length;
			return new Double(ave).toString();
		}
		else if(FillType==MOSTFREQUENTFILL)
		{
			ArrayList<Double> orglist=new ArrayList<Double>();
			int[] times_array=new int[value.length];
			for(int i=0;i<value.length;i++)
			{
				int k=0;
				for(;k<orglist.size();k++)
				{
					if(Double.compare(orglist.get(k),value[i])==0)
					{
						times_array[k]++;
						break;
					}
				}
				if(k==orglist.size())
				{
					int size=orglist.size();
					orglist.add(size,value[i]);
					times_array[size]=1;
				}
			}
			int maxindex=0;
			for(int i=1;i<orglist.size();i++)
			{
				if(Double.compare(times_array[i],times_array[maxindex])>0)
					maxindex=i;
			}
			return orglist.get(maxindex).toString();
		}
		else if(FillType==LEASTFREQUENTFILL)
		{
			ArrayList<Double> orglist=new ArrayList<Double>();
			int[] times_array=new int[value.length];
			for(int i=0;i<value.length;i++)
			{
				int k=0;
				for(;k<orglist.size();k++)
				{
					if(Double.compare(orglist.get(k),value[i])==0)
					{
						times_array[k]++;
						break;
					}
				}
				if(k==orglist.size())
				{
					int size=orglist.size();
					orglist.add(size,value[i]);
					times_array[size]=1;
				}
			}
			int minindex=0;
			for(int i=1;i<orglist.size();i++)
			{
				if(times_array[i]<times_array[minindex])
					minindex=i;
			}
			return orglist.get(minindex).toString();
		}
		else
			return null;
	}

}
